package gold.digger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/3/1.
 */
public class LC986 {


    /*
     * @param 此题目直接用别人代码，自己只理解思想
     * @return:
     */
    public int[][] intervalIntersection(int[][] A, int[][] B) {
        int i = 0, j = 0;
        List<int[]> res = new ArrayList<>();
        while (i < A.length && j < B.length) {
            // if intersection exist
            int a1 = A[i][0], a2 = A[i][1];
            int b1 = B[j][0], b2 = B[j][1];

            if (b2 >= a1 && a2 >= b1) {
                int[] curRes = new int[2];
                curRes[0] = Math.max(a1, b1);
                curRes[1] = Math.min(a2, b2);
                res.add(curRes);
            }

            if (b2 < a2) {
                j++;
            } else {
                i++;
            }
        }

        return (int[][]) res.toArray(new int[0][]);
    }

    public static void main(String[] args) {
        LC986 an = new LC986();
        System.out.println(an.intervalIntersection(null, null));
    }
}
